<template>
  <el-container class="app">
    <el-header class="header" style="height:auto">
      <!-- 头部导航栏 -->
      <div class="nav">
        <div class="logo">
          <img src="@/assets/images/musicLogo.png" alt="" />
        </div>
        <ul class="keywords">
          <template v-for="(item, index) in keywords">
            <router-link
              class="li"
              :to="item.path"
              :key="index"
              :class="{ active: $route.fullPath === item.path }"
            >
              {{ item.name }}
            </router-link>
          </template>
        </ul>
        <div class="search-bar" v-clickoutside="closeSearchList">
          <input
            type="text"
            v-model="searchValue"
            @focus="searchFocus"
            placeholder="搜索"
            maxlength="20"
            @keydown.enter="toSearchDetail"
          />
          <i class="iconfont icon-search" @click.stop="toSearchDetail"></i>
          <transition name="el-fade-in-linear">
            <div class="search-dropdown" v-show="showHistoryBox">
              <div class="history-search" v-show="historyList.length">
                <div class="title">
                  <span>历史搜索</span>
                  <i class="iconfont icon-remove" @click="removeHistory"></i>
                </div>
                <ul class="history-value clearfix">
                  <li
                    v-for="(item, index) in historyList"
                    :key="index"
                    @click.stop="toSearch(item)"
                  >
                    {{ item }}
                  </li>
                </ul>
              </div>
              <div class="hot-search">
                <div class="title">热门搜索</div>
                <ul>
                  <li
                    v-for="(item, index) in hotSearchList"
                    :key="index"
                    @click.stop="hotSearch(item.searchWord)"
                  >
                    <div class="left">{{ index + 1 }}</div>
                    <div class="right">
                      <div class="name">{{ item.searchWord }}</div>
                      <div class="desc overflow2">{{ item.content }}</div>
                    </div>
                  </li>
                </ul>
              </div>
            </div>
          </transition>
        </div>
        <div class="not-login" v-if="!userInfo" @click="showDialog.show = true">
          登录 / 注册
        </div>
        <div
          @mouseenter="showSet"
          @mouseleave="hideSet"
          class="user-avatar"
          v-else
        >
          <img :src="userInfo.avatarUrl" alt="" />
          <el-collapse-transition>
            <ul class="settings" v-show="showSettings">
              <li><i class="iconfont icon-user"></i> <span>我的主页</span></li>
              <li @click.stop="logout">
                <i class="iconfont icon-tuichudenglu"></i> <span>退出登录</span>
              </li>
            </ul>
          </el-collapse-transition>
        </div>
      </div>
    </el-header>
    <el-main class="main">
      <router-view></router-view>
    </el-main>
    <el-footer class="footer">Footer</el-footer>
    <loginRegistry :showDialog="showDialog"></loginRegistry>
    <muiscplayer></muiscplayer>
  </el-container>
</template>
<script>
import loginRegistry from '@/components/login_registry.vue'
import {
  getUserId,
  removeUserId,
  removeToken,
  getHistoryList,
  setHistoryList,
  removeHistoryList
} from '@/utlis/storage'
import { getUserInfo } from '@/api/user'
import { mapState, mapMutations } from 'vuex'
import { getHotSearch } from '@/api/search'
import muiscplayer from '@/components/muisc_player'
const clickoutside = {
  // 初始化指令
  bind (el, binding, vnode) {
    function documentHandler (e) {
      // 这里判断点击的元素是否是本身，是本身，则返回
      if (el.contains(e.target)) {
        return false
      }
      // 判断指令中是否绑定了函数
      if (binding.expression) {
        // 如果绑定了函数 则调用那个函数，此处binding.value就是handleClose方法
        binding.value(e)
      }
    }
    // 给当前元素绑定个私有变量，方便在unbind中可以解除事件监听
    el.__vueClickOutside__ = documentHandler
    document.addEventListener('click', documentHandler)
  },
  update () {},
  unbind (el, binding) {
    // 解除事件监听
    document.removeEventListener('click', el.__vueClickOutside__)
    delete el.__vueClickOutside__
  }
}
export default {
  name: 'app',
  components: {
    loginRegistry,
    muiscplayer
  },
  directives: { clickoutside },
  data () {
    return {
      // 热搜歌曲列表
      hotSearchList: [],
      // 历史记录数组
      historyList: getHistoryList() || [],
      // 显示历史记录
      showHistoryBox: false,
      // 关键字们
      keywords: [
        { name: '发现音乐', path: '/discover' },
        { name: '排行榜', path: '/rank_list' },
        { name: '歌单', path: '/song_list' },
        { name: '歌手', path: '/singer' },
        { name: '新碟上架', path: '/album' },
        { name: 'MV', path: '/mv' }
      ],

      // 是否显示下拉框
      showSettings: false,
      searchValue: '',
      showDialog: {
        show: false
      }
    }
  },
  methods: {
    ...mapMutations(['SETUSERINFO']),

    // 点击搜索面板关键字去搜索页
    toSearch (item) {
      this.searchValue = item
      this.toSearchDetail()
    },
    hotSearch (keyWord) {
      this.searchValue = keyWord
      this.toSearchDetail()
    },
    // 删除历史记录
    removeHistory () {
      this.historyList = []
      removeHistoryList()
      this.showHistoryBox = true
    },
    // 触发搜索
    toSearchDetail () {
      if (this.searchValue.trim() === '') {
        return
      }
      this.historyList.unshift(this.searchValue.trim())
      this.historyList = [...new Set(this.historyList)]
      if (this.historyList.length >= 7) {
        this.historyList.splice(this.historyList.length - 1, 1)
      }
      setHistoryList(this.historyList)
      this.showHistoryBox = false
      this.$router.push('/search?keywords=' + this.searchValue.trim())
    },
    // 输入框获得焦点
    searchFocus () {
      this.showHistoryBox = true
    },
    // 关闭搜索列表
    closeSearchList () {
      this.showHistoryBox = false
    },

    // 退出登录事件
    logout () {
      this.showSettings = false
      // 删除token userId 清空用户信息
      removeUserId()
      removeToken()
      this.SETUSERINFO('')
    },
    // 鼠标移动事件
    showSet (e) {
      this.showSettings = true
    },
    hideSet () {
      this.showSettings = false
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  created () {
    // 偷偷的获取用户信息
    if (getUserId()) {
      getUserInfo({ uid: getUserId() })
        .then(res => {
          this.SETUSERINFO(res.profile)
        })
        .catch(err => {
          window.console.log(err)
        })
    }
    getHotSearch().then(res => {
      this.hotSearchList = res.data
    })
  }
}
</script>
<style lang="less">
// 滚动条样式
body::-webkit-scrollbar {
  width: 10px;
  background: #f2f2f2;
  border-radius: 10px; /*外层轨道*/
}
body::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: #c2c2c2; /*内层轨道*/
}
// el改的样式
.el-loading-mask {
  .el-loading-spinner {
    .circular {
      .circle {
        stroke: #121212;
      }
    }
  }
}
.el-form-item.is-success .el-input__inner,
.el-form-item.is-success .el-input__inner:focus {
  border-color: #67c23a;
}

.el-input {
  .el-input__inner:focus {
    border-color: #333;
  }
}
.el-select {
  .el-input__inner:focus {
    border-color: #67c23a !important;
  }
}
.app {
  min-height: 110%;
  background-color: #f5f5f5;

  .header {
    padding: 0;
    background-color: #121212;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 2020;
    box-shadow: 0 2px 5px 0 #121212;
    // 导航栏
    .nav {
      position: relative;
      width: 1350px;
      margin: 0 auto;
      color: #fff;
      height: 80px;
      .keywords {
        position: absolute;
        left: 150px;
        height: 100%;
        .li {
          cursor: pointer;
          float: left;
          padding: 0 20px;
          line-height: 80px;
          font-size: 16px;
          color: #ccc;
          text-decoration: none;
          &.active {
            position: relative;
            color: @main-font-color;
            background-color: #000;
            &::after {
              content: '';
              position: absolute;
              bottom: 1px;
              left: 50%;
              margin-left: -6px;
              width: 0;
              height: 0;
              border-left: 6px solid transparent;
              border-right: 6px solid transparent;
              border-bottom: 6px solid @main-font-color;
            }
          }
          &:hover {
            color: @main-font-color;
            background-color: #000;
          }
        }
      }
      .logo {
        position: absolute;
        top: 23px;
        width: 60px;
        img {
          width: 60px;
          height: 34px;
        }
      }
      .search-bar {
        position: absolute;
        top: 26px;
        right: 180px;
        width: 260px;
        height: 30px;
        .search-dropdown {
          width: 100%;
          border-radius: 4px;
          background-color: #fff;
          padding: 10px 0;
          color: #000;
          font-size: 16px;
          height: 340px;
          overflow: auto;
          &::-webkit-scrollbar {
            width: 10px;
            background: #f2f2f2;
            border-radius: 10px; /*外层轨道*/
          }
          &::-webkit-scrollbar-thumb {
            border-radius: 10px;
            background: #c2c2c2; /*内层轨道*/
          }
          .history-search {
            padding-right: 10px;
            .title {
              padding: 0 0 0 10px;
              display: flex;
              justify-content: space-between;
              align-items: center;
              span {
                color: #999;
              }
              i.iconfont {
                font-size: 14px;
                cursor: pointer;
                &:hover {
                  color: #ff3a3a;
                }
              }
            }
            .history-value {
              li {
                cursor: pointer;
                margin-top: 6px;
                margin-left: 10px;
                float: left;
                font-size: 13px;
                padding: 3px 6px;
                background-color: #585858;
                border-radius: 4px;
                color: #fff;
              }
            }
          }
          .hot-search {
            margin-top: 10px;
            .title {
              padding: 0 10px;
              color: #999;
              margin-bottom: 5px;
            }
            ul {
              li {
                padding: 5px 10px;
                display: flex;
                align-items: center;
                width: 100%;
                cursor: pointer;
                &:hover {
                  background-color: #ededed;
                  .right {
                    .name {
                      font-size: 18px;
                    }
                  }
                }

                &:nth-child(-n + 3) .left {
                  color: #ff3a3a;
                }
                .left {
                  width: 30px;
                  color: #999999;
                  font-size: 16px;
                }
                .right {
                  flex: 1;
                  .name {
                    font-size: 1;
                    font-size: 14px;
                  }
                  .desc {
                    color: #999999;
                    font-size: 12px;
                  }
                }
              }
            }
          }
        }
        input {
          outline: none;
          box-sizing: border-box;
          padding: 0 15px;
          width: 100%;
          height: 100%;
          border-radius: 15px;
          border: 1px solid #121212;
          color: #606266;
        }
        ::-webkit-input-placeholder {
          color: #c0c4cc;
          font-size: 12px;
        }
        .icon-search {
          cursor: pointer;
          position: absolute;
          font-size: 18px;
          top: 6px;
          right: 10px;
          color: #909399;
          &:hover {
            font-weight: 600;
            color: #e91e63;
          }
        }
      }

      .not-login {
        cursor: pointer;
        position: absolute;
        top: 26px;
        right: 0;
        height: 30px;
        line-height: 30px;
        padding: 0 8px;
        font-size: 14px;
        color: black;
        background-color: #63dbc1;
        border-radius: 8px;
        font-weight: 600;
      }
      .user-avatar {
        cursor: pointer;
        position: absolute;
        top: 17px;
        right: 37px;
        bottom: -10px;
        text-align: center;
        img {
          box-sizing: border-box;
          width: 46px;
          height: 46px;
          border-radius: 50%;
          border: 2px solid #63dbc1;
        }
        ul.settings {
          position: absolute;
          width: 120px;
          top: 66px;
          left: -37px;
          list-style: none;
          background-color: @main-bg;
          border-radius: 4px;
          overflow: hidden;
          li {
            font-size: 14px;
            text-align: center;
            height: 40px;
            line-height: 40px;
            color: #ccc;
            i {
              font-size: 16px;
              margin-right: 6px;
            }
            &:hover {
              background-color: #000;
              color: @main-font-color;
            }
          }
        }
      }
    }
  }
  .main {
    margin-top: 80px;
    overflow: visible;
    padding: 0;
    background-color: #fff;
  }
  .footer {
    margin-bottom: 66px;
    background-color: blue;
  }
}
</style>
